﻿using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Reflection;
using System.Configuration;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.Common; // DataSource definition
using IBatisNet.Common.Utilities; // ScriptRunner definition
using IBatisNet.DataMapper; // SqlMap API
using System.Collections.Specialized;


namespace ScenarioFramework.WorkflowEngine.Scheduler.Data
{
    public class Helper
    {
        private static volatile ISqlMapper _mapper = null;

        protected void Configure(object obj)
        {
            _mapper = null;
        }

        protected void InitMapper()
        {
            ConfigureHandler handler = new ConfigureHandler(Configure);
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            NameValueCollection properties = new NameValueCollection();
            properties.Add("collection2Namespace", "IBatisNet.DataMapper.Test.Domain.LineItemCollection2, IBatisNet.DataMapper.Test");
            properties.Add("nullableInt", "int?");
            builder.Properties = properties;
            try
            {
                String sPath = String.Format("sqlmap.config");
                _mapper = builder.Configure(sPath);
            }
            catch (Exception ex)
            {
                Exception e = ex;
                while (e != null)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace.ToString());
                    e = e.InnerException;

                }
                throw;
            }
           
        }

        /*public SqlMapper Mapper()
        {
            return (SqlMapper)IBatisNet.DataMapper.Mapper.Instance();
        }*/

        public ISqlMapper Mapper()
        {
            if (_mapper == null)
            {
                lock (typeof(SqlMapper))
                {
                    if (_mapper == null) // double-check
                    {
                        InitMapper();
                    }
                }
            }
            return _mapper;
        }

    }
}